//  获取录屏列表
let getScreenList = async (ctx, next) => {
  let screenData = await ctx.db.querys(`select * from screen`)
  let classData = await ctx.db.querys(`select * from class`)
  let userData = await ctx.db.querys(`select * from user`)
  if (!screenData.length) {
    ctx.body = { code: 0, msg: "暂无数据" }
    return
  }
  let arr = [];
  screenData.forEach(item => {
    let obj = {};
    obj.screen = item;
    obj.class = classData.filter(jtem => jtem.classid === item.classid);
    obj.user = userData.filter(jtem => jtem.userid === item.userid);
    arr.push(obj)
  })
  ctx.body = { code: 1, msg: '录屏列表数据获取成功', data: arr }
}


//  录屏列表模糊搜索
let searchScreen = async (ctx, next) => {
  const { classid } = ctx.request.body;
  let screenData = await ctx.db.querys(`select * from screen`)
  let classData = await ctx.db.querys(`select * from class`)
  let userData = await ctx.db.querys(`select * from user`)
  if (!screenData.length) {
    return ctx.body = { code: 0, msg: '暂无数据', data: [] }
  }
  let arr = [];
  if (classid !== "all") {
    screenData = screenData.filter(item => item.classid === classid)
    screenData.forEach(item => {
      let obj = {};
      obj.screen = item;
      obj.class = classData.filter(jtem => jtem.classid === item.classid);
      obj.user = userData.filter(jtem => jtem.userid === item.userid);
      arr.push(obj)
    })
    screenData = arr
  } else {
    screenData.forEach(item => {
      let obj = {};
      obj.screen = item;
      obj.class = classData.filter(jtem => jtem.classid === item.classid);
      obj.user = userData.filter(jtem => jtem.userid === item.userid);
      arr.push(obj)
    })
    screenData = arr
  }
  ctx.body = { code: 1, msg: '数据请求成功', data: screenData }
}



// 添加录屏列表
let addScreen = async (ctx, next) => {
  let isOpr = await ctx.isJudge(ctx, 'A');

  // 判断当前用户是否可以操作
  if (!isOpr) {
    ctx.body = { code: 0, msg: '您无权操作' }
    return
  }
  // 如果权限够  就可以添加
  const { classid, type, filepath, filepwd, miaoshu, userid } = ctx.request.body;
  let createTime = new Date().getTime() + ''
  let results = await ctx.db.querys(`insert into screen (classid,type,filepath,filepwd,miaoshu,userid,createTime) values ('${classid}','${type}','${filepath}','${filepwd}','${miaoshu}',${Number(userid)},'${createTime}')`)

  if (results.affectedRows === 1) {
    ctx.body = { code: 1, msg: '添加成功' }
    return
  }
  ctx.body = { code: 0, msg: '添加失败' }
}



export default {
  getScreenList, searchScreen, addScreen
}